/**
 * @file 
 * This file has configures BOINC Clients and their associated projects
 * 
 * @author Kenneth Dixon
 * @version 0.2
 * 
 * Licence:
 * 
 * MythBOINC is a BOINC plugin for MythTV, it allows for BOINC Client management via the MythTV interface.
 * Copyright (C) 2008  Kenneth Dixon
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#ifndef MYTHBOINCCONFIG_H
#define MYTHBOINCCONFIG_H

#include <mythtv/settings.h> 


/*************************************************************************************************/

class MythBoincClientsConfig : public QObject, public ConfigurationWizard 
{
    Q_OBJECT
    
    public:
    
    
        /**
        * This constructor initialises the MythBOINC configuration page
        * Initialy it deals boinc client connections
        */
        MythBoincClientsConfig();
     
        
        /**
        * This destructor tidies up
        */
        ~MythBoincClientsConfig();
        
        
    private:
        
        
        /**
        * Once deturmined, this string holds the path to the boinc_cmd binary
        *   
        * @var cmd
        */ 
        string cmd;
    
    
        /**
        * This is the root configurationgroup, to be attached to the configurationwizard 
        * 
        * @var boincClientVCG
        */   
        VerticalConfigurationGroup *boincClientVCG;
        
        
        /**
        * This vector holds pointers to the IP Address text fields, to allow access if required
        *  
        * @var BoincClientIpTXTVEC
        */ 
        vector<Setting*> BoincClientIpTXTVEC;
        
        
        /**
        * This vector holds pointers to the password text fields, to allow access if required
        * 
        * @var BoincClientPwdTXTVEC
        */         
        vector<Setting*> BoincClientPwdTXTVEC;
        
        
        /**
        * This vector holds pointers to the edit buttons, to allow access if required
        * 
        * @var BoincClientEditBTNVEC
        */         
        vector<Setting*> BoincClientEditBTNVEC;


//Attributes
/************************************************/
//Methods


        /**
        * Converts an integer to a string
        * 
        * @param inInt - integer to be cast
        * @return - integer as a string 
        */  
        string IntToString(int);
        
        
        /**
        * This method produces a editable text box which allows the user to enter 
        * an IP address for a boinc client
        * 
        * @param ID - determines which boinc client being edited
        * @returns - Returns an input text box to be attched to the vertical cofiguration group
        */
        HostLineEdit *BoincClientIpTXT(int);
 
        
        /**
        * This method produces a editable text box which allows the user to enter 
        * a password for a boinc client
        * 
        * @param ID - determines which boinc client is being edited
        * @returns - Returns an input text box to be attched to the vertical cofiguration group
        */
        HostLineEdit *BoincClientPwdTXT(int);
 
        
        /**
        * Creates an edit button for a boinc client setup to edit a current client
        * 
        * @param ID - determines which boinc client is to be edited
        * @returns - An edit button for a boinc client setup
        */
        ButtonSetting *BoincClientEditBTN(int);
 
 
        /**
        * Loads initial items onto page
        * 
        */ 
        void LoadPage(int);
 
        
        /**
        * Creates a new client group
        * 
        * @param ID - which client is to be added
        */  
        void AddClient(int);
 
        
        /**
        * Attaches the client group to the main window
        * 
        * @param ID - which client is to be added
        */         
        void AttachClient(int);

        
        /**
        * Tests a Boinc Clients connection
        * 
        * @param ID - ID of client to be tested
        * @return - if sucessfull connection, then true else false 
        */         
        bool TestClientConnection(QString);
 
        
        /**
        * Produces a popup dialog containing specifics of the event (error, etc)
        * 
        * @param msg - The message string to be displayed
        */       
        void DialogBox(QString);
 
        
    private slots:
    
    
        /**
        * This method is the event handler for an edit button press.
        * 
        * @param ID - The button for which boinc client config to be edited
        */
        void editBtnPress(QString);
               
};

/*************************************************************************************************/

class MythBoincClientConfig : public QObject, public ConfigurationWizard 
{
    Q_OBJECT
    
    public:
    
    
        /**
        * This constructor initialises the individual client configuration pages
        * 
        */
        MythBoincClientConfig(QString, QString, QString);
        
        
        /**
        * This destructor tidies up
        * 
        */
        ~MythBoincClientConfig();
       
       
    private:
    
    
        /**
        * Holds the location of the boinc_cmd binary, probibly /usr/bin/boinc_cmd  
        * 
        * @var cmd
        */     
        QString cmd;
        
        
        /**
        * Holds the IP address of the boinc client being accessed 
        * 
        * @var ip
        */     
        QString ip;
        
        
        /**
        *  Holds the password for boinc client being accessed
        * 
        * @var pwd
        */ 
        QString pwd;
        
        
        /**
        * @var ProjectNameTXTVEC
        */ 
        vector<Setting*> ProjectNameTXTVEC;
        
        
        /**
        * @var ProjectDetachCBXVEC
        */         
        vector<HostCheckBox*> ProjectDetachCBXVEC;
        
        
        /**
        * This method will run through and detach any selected projects
        * 
        */         
        void DetachSelected();
          
               
        /**
        * This method provides a check box for each attached project,
        * it will detach a the project if checked and the finish button is pressed.
        * 
        * @param ID - To deturmine which project is to be actioned 
        * @returns - Returns an input text box to be attched to the vertical cofiguration group
        */                
        HostCheckBox *ProjectDetachCBX(int);
        
        
        /**
        * This method provides a button to detach selected projects
        * 
        * @returns - A button to detach projects
        */  
        ButtonSetting *ProjectDetachBTN();
        
        
        /**
        * Converts an integer to a string
        * 
        * @param inInt - integer to be cast
        * @return - integer as a string 
        */         
        string IntToString(int);
        
        
        /**
        * Produces a configuration group containing attached projects
        * 
        * @return - a verticalconfigurationgroup with attached projects 
        */        
        VerticalConfigurationGroup *GetAttachedProjects();
        
        
        /**
        * This method produces a textbox for the project name
        * 
        * @param inName - to load the extracted value
        * @param ID - To deturmine which project is to be actioned 
        * @returns - Returns a text box to be attched to the vertical cofiguration group
        */         
        HostLineEdit *ProjectDetachNameTXT(QString, int);
        
        
        /**
        * This method extracts the names of any attached projects
        * 
        * @returns - A vector containing extracted project names
        */         
        vector<QString> GetAttachedURLs();
        
        
        /**
        * This method initialises the setting page, adding text inputs, buttons etc 
        * for attaching BOINC projects
        * 
        */
        void LoadAttachPage();
        
        
        /**
        * This method initialises the setting page, adding text inputs, buttons etc 
        * for detaching BOINC projects
        * 
        */
        void LoadDetachPage();  
        
        
        /**
        * Adds project info to vector, ready to be loaded
        * 
        */       
        void PopulateProjectDetails();
        
        
        /**
        * Is the global variable containing project details  
        * 
        * @var projectDetails
        */         
        vector<vector<QString> > projectDetails;
        
                
        /**
        * Is the global variable holds the project master URL, and label  
        * 
        * @var ProjectAttachURL
        */         
        HostComboBox *ProjectAttachURL;
        
        
        /**
        * Is the global variable holds the a custom project URL
        * 
        * @var ProjectAttachCustURL
        */          
        HostLineEdit *ProjectAttachCustURL;
        
        
        /**
        * Is the global variable holds the email text 
        * 
        * @var ProjectAttachEmail
        */          
        HostLineEdit *ProjectAttachEmail;
        
        
        /**
        * Is the global variable holds the password text  
        * 
        * @var ProjectAttachPwd
        */   
        HostLineEdit *ProjectAttachUsr;
        
        
        /**
        * Is the global variable holds the user name   
        * 
        * @var ProjectAttachPwd
        */         
        HostLineEdit *ProjectAttachPwd;        
            
              
        /**
        * This method procuces a combobox conataining a list of valid BOINC Projects
        * 
        * @returns - Combobox containing master project URL's  
        */
        HostComboBox *ProjectAttachURLCBX();
        
        
        /**
        * This method procuces a textbox containing a custom project URL
        * 
        * @returns - Textbox holding a custom project URL 
        */        
        HostLineEdit *ProjectAttachCustURLTXT();
        
           
        /**
        * This method procuces a textbox to hold the email login
        * 
        * @returns - A textbox to contain the email login 
        */      
        HostLineEdit *ProjectAttachEmailTXT();       
        
        
        /**
        * This method procuces a textbox to hold the password
        * 
        * @returns - A textbox to contain the password for the account
        */        
        HostLineEdit *ProjectAttachPwdTXT();  
        
        
        /**
        * This method procuces a textbox to hold the user name
        * 
        * @returns - A textbox to contain the user name for the account
        */        
        HostLineEdit *ProjectAttachUsrTXT();         
             
               
        /**
        * This method procuces a button which when triggered will attach the defined project
        * 
        * @returns - An 'attach project' button 
        */        
        ButtonSetting *ProjectAttachBTN();
        
        
        /**
        * This method will lookup and account and return an account key, 
        * if an account does not exist then one will be created and an account key issued
        */        
        void CreateProjectAccount();   
        
        
        /**
        * This method will attach a project to the selected BOINC Client
        * 
        * @param inAccountKey - The account key is required to attach the project to a specific account
        */         
        void AttachProject(string);
        
           
        /**
        * This is a generic OK popup box to display any user relevant messages (i.e. errors)
        * 
        * @param msg - The message to be displayed inthe popup box
        */         
        void DialogBox(QString);
        
        
        /**
        * This method initialises the setting page, adding text inputs, buttons etc
        * for attaching and detaching BOINC Managers
        * 
        */
        void LoadMgrPage();
        
        
        /**
        * This method produces a editable text box which allows the user to enter 
        * the URL of a BOINC Client Manager site
        * 
        * @returns - Returns an input text box to be attched to the vertical cofiguration group
        */
        HostLineEdit *BoincManagerURLTXT();
        
        
        /**
        * This method produces a editable text box which allows the user to enter 
        * the name of the manager account
        * 
        * @returns - Returns an input text box to be attched to the vertical cofiguration group
        */        
        HostLineEdit *BoincManagerNameTXT();
        
        
        /**
        * This method produces a editable text box which allows the user to enter 
        * the password for the manager account
        * 
        * @returns - Returns an input text box to be attched to the vertical cofiguration group
        */        
        HostLineEdit *BoincManagerPwdTXT();
        
        
        /**
        * Creates a button for attaching the specified BOINC Client Manager
        * 
        * @returns - An Attach button for a boinc client manager setup
        */
        ButtonSetting *AttachMgrBTN();
        
        
        /**
        * Creates a button for deteching any attached BOINC Client Manager
        * 
        * @returns - An Detattach button for a boinc client manager setup
        */       
        ButtonSetting *DetachMgrBTN();
        
        
    private slots:
    
    
        /**
        * This method is the event handler for a button press.
        * 
        * @param ID - To determine which button has been pressed
        */
        void BtnPress(QString);
 
 
        /**
        * This method is the event handler for a change in the project combobox.
        * 
        * @param ID - The value of the now selected project
        */
        void projectChanged(const QString&);
            
};



#endif
